<?php

namespace app\adminapi\controller\v1\posts;

use app\adminapi\controller\AuthController;
use app\dao\system\attachment\SystemAttachmentDao;
use app\generator\CuteNicknameGenerator;
use app\Request;
use app\services\posts\PostCommentsServices;
use app\services\posts\PostsServices;
use app\services\user\UserServices;
use think\facade\App;

/**
 * 帖子
 * Class PostController
 * @package app\adminapi\controller\v1\posts
 */
class PostController extends AuthController
{
    /**
     * PostsServices constructor.
     * @param App $app
     * @param PostsServices $service
     * @method temp
     */
    public function __construct(App $app, PostsServices $service)
    {
        parent::__construct($app);
        $this->services = $service;
    }

    /**
     * 分页获取帖子列表
     * @param Request $request
     * @return mixed
     * @throws \ReflectionException
     */
    public function pageList(Request $request) {
        $where = $request->getMore([
            ['nickname', ''],
            ['status', 0],
            ['post_type', 0]
        ]);
        $dataList = $this->services->pageListAdmin($where);
        return app("json")->success("查询成功", $dataList);
    }

    /**
     * 修改帖子状态
     * @param Request $request
     * @return mixed
     */
    public function status(Request $request){
        $where = $request->postMore([
            ['id', 0],
            ['status', 0],
            ['ban_text', '']
        ]);

        if ($where['status'] == 2 && $where['ban_text'] == ''){
            return app("json")->fail("请填写禁用理由");
        }

        $this->services->update($where['id'], $where);
        return app("json")->success("修改成功");
    }

    /**
     * 删除帖子
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request) {
        $where = $request->postMore([
           ['ids' , []]
        ]);

        foreach ($where['ids'] as $id) {
            $this->services->update($id, ['is_del' => 1, 'ban_text' => '管理员删除']);
        }

        return app("json")->success("删除成功");
    }

    /**
     * 查询帖子评论
     * @param Request $request
     * @return mixed
     */
    public function commentList(Request $request) {
        $where = $request->getMore([
            ['post_id', 0]
        ]);
        /** @var PostCommentsServices $postCommentsServices */
        $postCommentsServices = app()->make(PostCommentsServices::class);
        $dataList = $postCommentsServices->pageListAdmin($where);
        return app("json")->success("查询成功", $dataList);
    }

    /***
     * 删除帖子评论
     * @param Request $request
     * @return mixed
     */
    public function commentDelete(Request $request) {
        $where = $request->postMore([
            ['ids', []]
        ]);
        /** @var PostCommentsServices $postCommentsServices */
        $postCommentsServices = app()->make(PostCommentsServices::class);
        foreach ($where['ids'] as $id) {
            $postComment = $postCommentsServices->getOne(['id' => $id]);
            if (isset($postComment)) {
                $postCommentsServices->update($id, ['is_del' => 1]);
                $post = $this->services->getOne(['id' => $postComment['post_id']]);
                $this->services->update($post['id'], ['comment_count' => $post['comment_count'] - 1]);
            }
        }
        return app("json")->success("删除成功");
    }

    public function add(Request $request) {
        /** @var CuteNicknameGenerator $cuteNicknameGenerator */
        $cuteNicknameGenerator = app()->make(CuteNicknameGenerator::class);
        /** @var SystemAttachmentDao $systemAttachmentDao */
        $systemAttachmentDao = app()->make(SystemAttachmentDao::class);
        /** @var UserServices $userServices */
        $userServices = app()->make(UserServices::class);
        $data = $request->postMore([
            ['is_virtual', 0],
            ['title', ''],
            ['content', ''],
            ['post_type', 1],
            ['tp_images',[]],
            ['status', 1],
            ['slider_image', []],
            ['is_anonymous', 0],
            ['vote_start_time',''],
            ['vote_end_time',''],
            ['browse', 0]
        ]);

        // 虚拟帖子
        if ($data['is_virtual'] == 1) {
            $systemAttachmentList = $systemAttachmentDao->selectList(['pid' => 18])->toArray();
            $name = $cuteNicknameGenerator->generate();
            $user = $userServices->save([
                'nickname' => $name,
                'real_name' => $name,
                'avatar' => "https://ttxzgz.gzsoft.fun".$systemAttachmentList[mt_rand(0, count($systemAttachmentList) - 1)]['att_dir'],
                'is_virtual' => 1
            ]);
            $data['user_id'] = $user['uid'];
        }
        else {
            $userList = $userServices->selectList(['is_virtual' => 1])->toArray();
            $data['user_id'] = $userList[mt_rand(0, count($userList) - 1)]['uid'];
        }
        $data['content'] = json_encode($data['content']);
        $data['slider_image'] = json_encode($data['slider_image']);
        $data['add_time'] = time();
        return app("json")->success("生成成功", [$this->services->save($data)]);
    }

    public function edit(Request $request) {
        $data = $request->postMore([
            ['id', 0],
            ['is_virtual', 0],
            ['title', ''],
            ['user_id', 0],
            ['content', ''],
            ['post_type', 1],
            ['status', 1],
            ['slider_image', []],
            ['is_anonymous', 0],
            ['browse', 0]

        ]);

        $data['content'] = json_encode($data['content']);
        $data['slider_image'] = json_encode($data['slider_image']);
        return app("json")->success("生成成功", [$this->services->update($data['id'],$data)]);
    }
}